package com.tistory.iam1492.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;


public class HttpCall {
	public static String executeGet(String query){
		HttpResponse response = null;
		
		if (query == null){
			return null;
		}

		Log.e("WOORAM", "QUERY:"+query);
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(query);
			
			get.setHeader("Accept","application/json");
			get.setHeader("Content-type","application/json");
			
			response = client.execute(get);
		
			HttpEntity entity = response.getEntity();
			return EntityUtils.toString(entity, "UTF-8");
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
/*	public static String executeNickPost(Context ctx, String url, String nickName){
		HttpResponse response = null;
		
		Log.e("WOORAM", "QUERY:"+url);
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			post.setHeader(HTTP.CONTENT_TYPE,"application/json");
			post.setHeader("Accept", "application/json");
			
			JSONObject obj = new JSONObject();

			obj.put("nickname",nickName);
			obj.put("imei",GeneratorUtil.generateIMEI(ctx));
			obj.put("blocked",0);
			obj.put("alert_count",0);

			Log.e("WOORAM", obj.toString());
			StringEntity se = new StringEntity(obj.toString(), HTTP.UTF_8);
			post.setEntity(se);
			response = client.execute(post);
			Log.e("WOORAM", EntityUtils.toString(response.getEntity()));
			return response.toString();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}*/
	public static String executeNickPost(String url, String nickname, String imei){
		HttpResponse response = null;
		
		Log.e("WOORAM", "QUERY:"+url);
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			post.setHeader(HTTP.CONTENT_TYPE,"application/json");
			post.setHeader("Accept", "application/json");
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
			post.setParams(params);
			
			JSONObject obj = new JSONObject();

			obj.put("nickname",nickname);
			obj.put("imei",imei);
			obj.put("blocked",0);
			obj.put("nickcount",0);
			obj.put("alert_count",0);

			Log.e("WOORAM", obj.toString());
			StringEntity se = new StringEntity(obj.toString(), HTTP.UTF_8);
			post.setEntity(se);
			response = client.execute(post);
			Log.e("WOORAM", EntityUtils.toString(response.getEntity()));
			return response.toString();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
/*	public static String executePost(String url, CommentBean bean){
		HttpResponse response = null;
		
		Log.e("WOORAM", "QUERY:"+url);
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			post.setHeader(HTTP.CONTENT_TYPE,"application/json");
			post.setHeader("Accept", "application/json");
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
			post.setParams(params);
			
			JSONObject obj = new JSONObject();

			obj.put("base",bean.base);
			obj.put("comment",bean.comment);
			obj.put("nickname",bean.nickname);
			obj.put("outcount",bean.outcount);
			obj.put("stage",bean.stage);
			obj.put("team",bean.team);
			obj.put("gameId",bean.game_id);
			obj.put("createdat",bean.createdat);
			obj.put("type", String.valueOf(bean.type));
			Log.e("WOORAM", obj.toString());
			StringEntity se = new StringEntity(obj.toString(), HTTP.UTF_8);
			post.setEntity(se);
			response = client.execute(post);
			Log.e("WOORAM", EntityUtils.toString(response.getEntity()));
			return response.toString();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}*/
	public static String execute(String query){
		StringBuilder sb = new StringBuilder();
		String response;
		try {
			URL url = new URL(query);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			if (conn != null){
				conn.setConnectTimeout(5000);
				conn.setUseCaches(false);
				if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){

					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream()));
					while(true){
						String line = br.readLine();
						if (line == null) break;
						sb.append(line);						
					}
					br.close();
				}
				else{
					conn.disconnect();
					return null;
				}
				conn.disconnect();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}		
		response = sb.toString();
		Log.e("WOORAM", response);
		return response;
	}
	public static String execute_Time(String query){
		Log.e("WOORAM", "execute_Time");
		StringBuilder sb = new StringBuilder();
		String response;
		try {
			URL url = new URL(query);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			if (conn != null){
				conn.setConnectTimeout(3000);
				conn.setReadTimeout(5000);
				conn.setUseCaches(false);
				int result = conn.getResponseCode();
				if(result==HttpURLConnection.HTTP_OK){
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream()));
					while(true){
						String line = br.readLine();
						if (line == null) break;
						sb.append(line);						
					}
					br.close();
				}
				else{
					conn.disconnect();
					return null;
				}
				conn.disconnect();
			}
		} catch (UnknownHostException e){
			e.printStackTrace();
			return null;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} 
		response = sb.toString();
		return response;
	}
}
